﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FinanSoft.Entities;
using System.Data.SqlClient;
using FinanSoft.Utilities;

namespace FinanSoft.DataLayer
{
    class DocumentTypeDataManager
    {
        #region Fields
        /// <summary>
        /// Atributo privado del tipo de la clase. Unica instancia de esta clase que existira
        /// </summary>
        private static DocumentTypeDataManager _instance;
        #endregion

        #region Constructors
        /// <summary>
        /// Constructor privado de la clase para permitir la implementacion del patron singleton
        /// </summary>
        private DocumentTypeDataManager()
        {

        }
        #endregion

        #region Methods
        /// <summary>
        /// Metodo que obtienen la instancia existente de la clase. Si esta aun no ha sido instanciada, la instancia.
        /// </summary>
        public static DocumentTypeDataManager GetInstance()
        {
            if (_instance == null)
                _instance = new DocumentTypeDataManager();
            return _instance;
        }

        /// <summary>
        /// Incrementa el número consecutivo de un tipo de documento.
        /// </summary>
        /// <param name="docName"></param>
        /// <returns></returns>
        public bool IncreaseDocumentConsecutiveNumber(string docName)
        {
            try
            {
                AppConfigReader appConfig = AppConfigReader.GetInstance();
                string spName = Constants.SP_NAME_INCREASE_CONSECUTIVE_NUMBER;
                SqlParameter[] parameters = { new SqlParameter(Constants.SP_PARAM_INCREASE_CONSECUTIVE_NUMBER_DOC_TYPE_NAME, docName) };
                SqlDataHelper dataHelper = new SqlDataHelper(appConfig.GetConnectionString(Constants.DB_CONN));
                SqlDataReader result = dataHelper.ExecuteStoredProcedure(spName, parameters);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// Obtiene los tipos de documento existentes en el sistema
        /// </summary>
        /// <returns> Una lista de objetos DocumentType correspondiente a todos los tipos de documento que se pueden asociar a un asiento</returns>
        public List<DocType> GetDocumentTypes()
        {
            try
            {
                List<DocType> typesList = new List<DocType>();
                AppConfigReader appConfig = AppConfigReader.GetInstance();
                string spName = Constants.SP_NAME_GET_DOCUMENT_TYPE;
                SqlParameter[] parameters = new SqlParameter[0];
                SqlDataHelper dataHelper = new SqlDataHelper(appConfig.GetConnectionString(Constants.DB_CONN));
                SqlDataReader result = dataHelper.ExecuteStoredProcedure(spName, parameters);
                while (result.Read())
                {                    
                    String description = (string)result[Constants.SP_COLUMN_GET_DOCUMENT_TYPE_ID_DESCRIPTION];
                    String name = (string)result[Constants.SP_COLUMN_GET_DOCUMENT_TYPE_ID_NAME];
                    int id = Convert.ToInt32(result[Constants.SP_COLUMN_GET_DOCUMENT_TYPE_ID]);
                    int consecutiveNumber = Convert.ToInt32(result[Constants.SP_COLUMN_GET_DOCUMENT_CONSECUTIVE_NUMBER]);
                    typesList.Add(new DocType(id, name, description, consecutiveNumber));
                }
                dataHelper.CloseReader();
                return typesList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion
    }
}
